﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：DelayAction
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/11 14:14:59
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System;
using TouchFish.ObjectPool;
using UnityEngine;

namespace TouchFish.NodeActionSystem
{
    /// <summary>
    /// 延时执行节点
    /// </summary>
    public class DelayAction : NodeAction, IPoolable
    {
        /// <summary>
        /// 延时执行时间
        /// </summary>
        public float DelayTime;

        /// <summary>
        /// 分配延时执行节点
        /// </summary>
        /// <param name="delayTime">延时时间</param>
        /// <param name="onEndCallback">回调函数（延时执行的方法）</param>
        /// <returns></returns>
        public static DelayAction Allocate(float delayTime, Action onEndCallback = null)
        {
            var retNode = SafeObjectPool<DelayAction>.Instance.Allocate();
            retNode.DelayTime = delayTime;
            retNode.OnEndedCallback = onEndCallback;
            return retNode;
        }

        /// <summary>
        /// 无参构造
        /// </summary>
        public DelayAction()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="delayTime">延时时间</param>
        public DelayAction(float delayTime)
        {
            DelayTime = delayTime;
        }

        /// <summary>
        /// 当前流逝的时间
        /// </summary>
        private float _elapsedTime = 0.0f;

        /// <summary>
        /// 重置
        /// </summary>
        protected override void OnReset()
        {
            _elapsedTime = 0.0f;
        }

        /// <summary>
        /// 执行
        /// </summary>
        protected override void OnExecute()
        {
            _elapsedTime += Time.deltaTime;
            Finished = _elapsedTime >= DelayTime;
        }

        /// <summary>
        /// 释放回收
        /// </summary>
        protected override void OnDispose()
        {
            SafeObjectPool<DelayAction>.Instance.Recycle(this);
        }

        public void OnRecycled()
        {
            DelayTime = 0.0f;
            Reset();
        }

        public bool IsRecycled { get; set; }
    }
}